﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WzCkDj : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess(string.Format("Jsc/WzCkDj.aspx?sCkLx={0}", Request.QueryString["sCkLx"]));
        }

        /// <summary>
        /// 加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (Page.IsPostBack) { return; }
            BtnImport.OnClientClick = "if(ShowDialog('FileImport.aspx',510,143)!=null){this.disabled=true;document.getElementById('HiddenField1').value=returnFileName;" + Page.ClientScript.GetPostBackEventReference(BtnImport, null) + ";}return false;";
            BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
            TextWhere.Focus();
        }

        /// <summary>
        /// 绑定刷新数据
        /// </summary>
        private void BindData(string sWhere, string nPage)
        {
            sWhere = (sWhere == null ? string.Empty : sWhere.Trim());
            string sCkLx = Request.QueryString["sCkLx"].Trim();
            string sDjr = (IsAccess(string.Format("编辑他人的{0}", sCkLx)) ? string.Empty : (string)Session["RealName"]);
            // 设置页数
            DataSet dsCount = BCkGl.GetWzCkDjByCxCount(sCkLx, sWhere, sDjr);
            Pager1.RecordCount = (dsCount == null ? 0 : (int)dsCount.Tables[0].Rows[0][0]);
            Pager1.CurrentPageIndex = (nPage == null ? 1 : int.Parse(nPage));
            // 查询数据
            DataSet ds = BCkGl.GetWzCkDjByCx(sCkLx, sWhere, sDjr, Pager1.PageSize, Pager1.CurrentPageIndex);
            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                PanelList1.Visible = true;
                PanelList2.Visible = false;
                return;
            }
            PanelList1.Visible = false;
            PanelList2.Visible = true;
            // 绑定表格
            GridData.DataSource = ds;
            GridData.DataBind();
            GridData.SelectedIndex = -1;
        }

        /// <summary>
        /// 导入
        /// </summary>
        protected void BtnImport_Click(object sender, EventArgs e)
        {
            MsgBox(BCkGl.ImportWzCkDj(HiddenField1.Value, Request.QueryString["sCkLx"].Trim(), (string)Session["RealName"]), true);
        }

        /// <summary>
        /// 操作[编辑/删除]
        /// </summary>
        protected void GridData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "bj")
            {
                int iSelectId = Convert.ToInt32(GridData.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
                for (int idx = 0; idx < GridData.DataKeys.Count; idx++)
                {
                    if ((int)GridData.DataKeys[idx].Value == iSelectId)
                    {
                        GridData.SelectedIndex = idx;
                        break;
                    }
                }
            }
            else if (e.CommandName == "sc")
            {
                if (ShowErr(BCkGl.DelWzCkDj((int)GridData.DataKeys[Convert.ToInt32(e.CommandArgument)].Value)))
                {
                    return;
                }
                BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
                GridData.SelectedIndex = (Convert.ToInt32(e.CommandArgument) < GridData.Rows.Count ? Convert.ToInt32(e.CommandArgument) : GridData.Rows.Count - 1);
            }
        }
    }
}